<?php
/**
 * image_manager_upload.php
 * Image Manager Admin Interface
 * Version 1.8
 *
 * @author  Boudewijn Sjouke <boudewijnsjouke@hotmail.com>
 * @copyright Copyright 2009-2011
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License V2.0
 */
 
// Do some preliminary work
require('includes/application_top.php');
@ini_set('error_reporting', 0);
@ini_set('display_errors', '1');
@error_reporting(0);

// Add an additional image or replace an existing image?
if (isset($_GET['imtype'])) $imtype = $_GET['imtype'];
else if (isset($_POST['imtype'])) $imtype = $_POST['imtype'];
else $imtype = "";

// Print the page header
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php print HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php print CHARSET; ?>">
<title><?php print TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
</head>
<body>
<script type="text/javascript">
<!--
	function edit_close() {
		window.opener.location.reload();
		window.close();
	}
	
	function check_suffix() {
		suffix = document.getElementById('IMsuffix');
		if (suffix.value == '') {
			alert('<?php print IM_ENTER_SUFFIX; ?>');
			return false;
		}
		return true;
	}
//-->
</script>

<div style="margin:20px;">
	<h1><?php print IM_HEADING_TITLE; ?></h1>
	<br /><h3><?php print ($imtype == "" ? IM_REUPLOAD_IMAGE : IM_UPLOAD_ADDITIONAL);?></h3>
<?php

if ($_GET["action"] == "upload") {
	$p = pathinfo(urldecode($_POST["imname"]));
	$image = new upload();
	$image->set_file('IMuploadfile');
	$image->set_destination(DIR_FS_CATALOG_IMAGES . 'uploads/');
	$image->set_output_messages('session');
	$image->set_extensions(strtolower($p['extension']));
	
	if ($imtype == "") {
		if ($image->parse()) {
			if ($image->save()) {
				if (!file_exists(DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['basename']) || @unlink(DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['basename'])) {
					if (!@copy($image->destination . $image->filename, DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['basename'])) $messageStack->add_session(IM_CANNOT_COPY, 'error');
					else {
						$messageStack->add_session(IM_REPLACED . ' ' . DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['basename'], 'success');
					}
					@unlink($image->destination . $image->filename);
				}
				else $messageStack->add_session(IM_CANNOT_DELETE . DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['basename'], 'error');
			}
			else $messageStack->add_session(IM_CANNOT_PROCESS, 'error');
		}
		else $messageStack->add_session(IM_CANNOT_PROCESS_PARAMS, 'error');
		print "\n<script type=\"text/javascript\">\n<!--\nedit_close();\n//-->\n</script>";
	}
	if ($imtype == "addim") {
		if (file_exists(DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['filename'] . '_' . $_POST['IMsuffix'] . '.' . $p['extension'])) {
			$messageStack->add_session(IM_ADD_IMG_EXISTS, 'error');
		}
		else {
			if ($image->parse()) {
				if ($image->save()) {
					if (!@copy($image->destination . $image->filename, DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['filename'] . '_' . $_POST['IMsuffix'] . '.' . $p['extension'])) $messageStack->add_session(IM_CANNOT_COPY, 'error');
					else {
						$messageStack->add_session(IM_ADD_SUCCESS . ' ' . DIR_FS_CATALOG_IMAGES . $p['dirname'] . '/' . $p['filename'] . '_' . $_POST['IMsuffix'] . '.' . $p['extension'], 'success');
					}
					@unlink($image->destination . $image->filename);
				}
				else $messageStack->add_session(IM_CANNOT_PROCESS, 'error');
			}
			else $messageStack->add_session(IM_CANNOT_PROCESS_PARAMS, 'error');
		}
		print "\n<script type=\"text/javascript\">\n<!--\nedit_close();\n//-->\n</script>";
	}
}
if (!isset($_GET["action"])) {
	$p = pathinfo(urldecode($_GET["imname"]));
	if ($imtype == "") {
		print zen_draw_form("uploadform", IM_UPLOAD_POPUP, 'action=upload', 'POST', 'enctype="multipart/form-data"');
		print zen_draw_hidden_field("imname", $_GET["imname"]);
		print IM_INPUT_UPLOAD . " <b>" . urldecode($_GET["imname"]) . "</b>.<br />";
		print IM_REQ_EXTENSION . " <b>" . strtolower($p["extension"]) . "</b>.<br /><br />";
		print zen_draw_file_field("IMuploadfile");
		print "<br /><br /><br />" . zen_image_submit(IM_FILENAME_SEND_BUTTON, IMAGE_SEND) . zen_draw_separator(IM_FILENAME_SPACER, '50px') . zen_image_submit(IM_FILENAME_CANCEL_BUTTON, IMAGE_CANCEL, 'onclick="window.close(); return false;"');
		print "</form>";
	}
	else if ($imtype == "addim") {
		print zen_draw_form("uploadform", IM_UPLOAD_POPUP, 'action=upload', 'POST', 'enctype="multipart/form-data"');
		print zen_draw_hidden_field("imname", $_GET["imname"]);
		print zen_draw_hidden_field("imtype", $_GET["imtype"]);
		print IM_INPUT_UPLOAD . "<br /><b>" . $p["dirname"] . "/" .  $p["filename"] . "&lt;_SUFFIX&gt;." . $p["extension"]. "</b>.<br /><br />";
		print zen_draw_file_field("IMuploadfile");
		print "<br /><br />";
		print IM_ADD_IMG_SUFFIX . '&nbsp;' . zen_draw_input_field('IMsuffix', '', 'size="6" id="IMsuffix"') . "&nbsp;" . IM_SUFFIX_ADD;
		print "<br /><br /><br />" . zen_image_submit(IM_FILENAME_SEND_BUTTON, IMAGE_SEND, 'onclick="return check_suffix();"') . zen_draw_separator(IM_FILENAME_SPACER, '50px') . zen_image_submit(IM_FILENAME_CANCEL_BUTTON, IMAGE_CANCEL, 'onclick="window.close(); return false;"');
		print "</form>";
	}
}
?>
</div>
</body>
</html>
<?php
	require(DIR_WS_INCLUDES . 'application_bottom.php');
?>